package com.atguigu.weixin.kit;

import java.io.IOException;
import java.nio.charset.UnsupportedCharsetException;

import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.atguigu.weixin.model.WeixinContext;
import com.atguigu.weixin.quartz.RefreshAccessTokenTask;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 微信核心功能
 * @author Administrator
 * 要在数据库里存起来
 */
public class WeixinKit {
	
	//替换token
	public static String replaceAccToken(String url){
		return url.replace("ACCESS_TOKEN", WeixinContext.getAccessToken());
	}
	
	/**
	 * 用户分组接口
	 * @param url
	 * @param data 数据
	 * @param type  类型
	 */
 public static String postReq(String url,String data,String type){
		CloseableHttpClient client=null;
		CloseableHttpResponse resp=null;
	    try {
	    	client=HttpClients.createDefault();
			url=url.replace("ACCESS_TOKEN",RefreshAccessTokenTask.at);
			HttpPost post=new HttpPost(url);
			post.addHeader("Content-Type",type);//"application/json;charset=UTF-8"
			StringEntity entity=new StringEntity(data, ContentType.create(type,"UTF-8"));
			post.setEntity(entity);
			resp=client.execute(post);
			int statusCode=resp.getStatusLine().getStatusCode();
			if(statusCode>=200&&statusCode<300){
				String str=EntityUtils.toString(resp.getEntity(),"UTF-8");
				System.out.println("发送请求的："+str);
				return str;
			}
		} catch (UnsupportedCharsetException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(client!=null) client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if(resp!=null) resp.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	    return null;
 }
 
 
 
	/**
	 * 
	 * @param url
	 * @param data 数据
	 * @param type  类型
	 */
public static String getReq(String url){
	CloseableHttpClient client=null;
	CloseableHttpResponse resp=null;
    try {
    	client=HttpClients.createDefault();
		url=url.replace("ACCESS_TOKEN",RefreshAccessTokenTask.at);
		HttpGet get=new HttpGet(url);
		resp=client.execute(get);
		int statusCode=resp.getStatusLine().getStatusCode();
		if(statusCode>=200&&statusCode<300){
			String str=EntityUtils.toString(resp.getEntity(),"UTF-8");
			System.out.println("发送请求的："+str);
			return str;
		}
	} catch (UnsupportedCharsetException e) {
		e.printStackTrace();
	} catch (ClientProtocolException e) {
		e.printStackTrace();
	} catch (ParseException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}finally{
		try {
			if(client!=null) client.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			if(resp!=null) resp.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
    return null;
}

/**
 * 检查我们的请求是否成功
 * 将json字符串像取xml一样取值
 * 例如：微信接口创建菜单
 * @return
 */
public static boolean checkRequestSuccess(String content){
	 try {
		ObjectMapper m = new ObjectMapper();  
		 JsonNode jn = m.readTree(content);  
		 if(!jn.has("errcode")) return true;
		 if(jn.get("errcode").asInt()==0)return true;
	} catch (JsonProcessingException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} 
	 return false;
}

/**
 * 检查我们的请求是否成功
 * 将json字符串像取xml一样取值
 * 例如：调用微信接口错误编号
 * @return
 */
public static int getRequestCode(String content){
	try {
		ObjectMapper m = new ObjectMapper();  
		 JsonNode jn = m.readTree(content);  
		 if(jn.has("errcode")) return jn.get("errcode").asInt();
	} catch (JsonProcessingException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} 
	 return -1;
	
}
/**
 * 检查我们的请求是否成功
 * 将json字符串像取xml一样取值
 * 例如：调用微信接口错误信息字符串
 * @return
 */
public static String getRequestMsg(String content) {
	try {
		ObjectMapper m = new ObjectMapper();  
		 JsonNode jn = m.readTree(content);  
		 if(jn.has("errcode")) return jn.get("errmsg").asText();
	} catch (JsonProcessingException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} 
	 return null;
}

public static String sendGet(String url) {
	HttpGet get = null;
	CloseableHttpResponse resp = null;
	CloseableHttpClient client = null;
	try {
		client = HttpClients.createDefault();
		get = new HttpGet(url);
		resp = client.execute(get);
		int statusCode = resp.getStatusLine().getStatusCode();
		if(statusCode>=200&&statusCode<300) {
			HttpEntity entity = resp.getEntity();
			String content = EntityUtils.toString(entity,"utf-8");
			return content;
		}
	} catch (ClientProtocolException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		try {
			if(resp!=null) resp.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			if(client!=null) client.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	return null;
}



public static String sendJsonPost(String url,String content) {
	return sendPost(url, content, "application/json");
}

public static String sendXmlPost(String url,String content) {
	return sendPost(url, content, "application/xml");
}

public static String sendPost(String url,String content,String type) {
	CloseableHttpClient client = null;
	CloseableHttpResponse resp = null;
	try {
		client = HttpClients.createDefault();
		HttpPost post = new HttpPost(url);
		post.addHeader("Content-type",type);
		StringEntity entity = new StringEntity(content, ContentType.create(type, "UTF-8"));
		post.setEntity(entity);
		resp = client.execute(post);
		int statusCode = resp.getStatusLine().getStatusCode();
		if(statusCode>=200&&statusCode<300) {
			String str = EntityUtils.toString(resp.getEntity());
			return str;
		}
	} catch (UnsupportedCharsetException e) {
		e.printStackTrace();
	} catch (ClientProtocolException e) {
		e.printStackTrace();
	} catch (ParseException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		try {
			if(client!=null) client.close();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		try {
			if(resp!=null) resp.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	return null;
}

}
